using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Winter.BackEnd.Models.Domain;
using System.Security.Principal;
using System.Web.Security;

namespace Winter.BackEnd.Controllers
{
    [Authorize, HandleError]
    public class PersonController : ApplicationController
    {

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index(int? id)
        {
            return View("Student", GetPersonByID(id));
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetPersonInfo(int? id)
        {
            var person = GetPersonByID(id);
            var result = new
                                   {
                                       sex = person.Sex,
                                       birthdate = person.BirthDate,
                                       email = person.Email,
                                       duties = person.Duties
                                   };
            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetPersonMessages(int? id)
        {
            return Json(GetPersonByID(id).RecievedMessages);
        }

        [NonAction]
        private IPerson GetPersonByID(int? id)
        {
            if (id == null)
            {
                var user = Repository.CurrentUser.Person.PersonInRole;
                return user;
            }
            return Repository.CurrentDataContext.Persons.SingleOrDefault(p => p.ID == id)
                    .PersonInRole;
        }
    }
}
